// 商城订单详情
import { get, post } from '@common/ajax';
import logger from '@common/logger';
import { TipSuccess, TipError } from '@common/tip';
import { SHOP_URL, INVENTORY_URL } from '@common/url';
import { PAY_ORDER_STATUS } from '@constants/pay';
import { autoShopPayment } from '@common/environment';
import { repalceTo } from '@common/utils';
import {
    GET_ORDER_DETAIL,
    GET_AFTER_SALE_ORDER_DETAIL,
    SET_AFTER_SALE_GOODS_INFO,
    SET_AFTER_SALE_ORDER_INFO,
    SET_AFTER_SALE_TYPE,
    GET_AVALIABLE_EXPRESS_LIST,
} from '@store/mutationType';

const orderDetail = {
    state: {
        orderDetail: {},
        // 售后相关
        afterSaleOrderDetail: {},
        afterSaleGoods: null,
        afterSaleOrderInfo: null,
        afterSaleType: 1,
        afterPageNumber: 1,
        afterPageSize: 10,
        // 可选快递列表
        expressList: [],
    },
    mutations: {
        GET_ORDER_DETAIL: (state, orderDetail) => {
            Object.assign(state, { orderDetail });
        },
        GET_AFTER_SALE_ORDER_DETAIL: (state, afterSaleOrderDetail) => {
            Object.assign(state, { afterSaleOrderDetail });
        },
        SET_AFTER_SALE_GOODS_INFO: (state, afterSaleGoods) => {
            Object.assign(state, { afterSaleGoods });
        },
        SET_AFTER_SALE_ORDER_INFO: (state, afterSaleOrderInfo) => {
            Object.assign(state, { afterSaleOrderInfo });
        },
        SET_AFTER_SALE_TYPE: (state, afterSaleType) => {
            Object.assign(state, { afterSaleType });
        },
        GET_AVALIABLE_EXPRESS_LIST: (state, expressList) => {
            Object.assign(state, { expressList });
        },
    },
    actions: {
        // 查询订单摘要
        getOrderAbstract: async ({ commit }, requestId) => {
            const res = await get(SHOP_URL.orderPrepay, {
                request_id: requestId,
            });
            logger.success('OrderDetail => 获取的摘要信息为：', res);
            return res;
        },
        // 唤起支付
        repayOrder: async ({ commit }, { requestId, isCardPay, orderNumber }) => {
            const res = await post(SHOP_URL.orderPay, {
                request_id: requestId,
                balance_pay: isCardPay,
            });
            logger.success('Review => 支付结果', res);
            switch(res.status) {
            case PAY_ORDER_STATUS.init:
                // 执行平台相关支付
                autoShopPayment(res.prepay_info, '', () => {});
                break;
            case PAY_ORDER_STATUS.success:
                // 支付成功，直接跳转到订单列表
                repalceTo('shop-order/list');
                break;
            case PAY_ORDER_STATUS.failed:
                TipError('支付失败，请重试');
                repalceTo('shop-order/list');
                break;
            case PAY_ORDER_STATUS.close:
                TipError('支付已关闭');
                repalceTo('shop-order/list');
                break;
            }
        },
        // 获取订单详情
        getOrderDetail: async ({ commit }, orderNumber) => {
            const res = await get(SHOP_URL.orderInfo, { order_number: orderNumber });
            logger.success('Order => 订单详情', res);
            commit(GET_ORDER_DETAIL, res);
        },
        // 确认收货
        confirmOrder: async ({ dispatch }, orderNumber) => {
            const res = await post(SHOP_URL.orderConfirm, {
                order_number: orderNumber,
            });
            dispatch('getOrderDetail', orderNumber);
            return res ? true : false;
        },
        // 取消订单
        cancelOrder: async () => {

        },
        // 设置售后商品信息
        setAfterSaleGoodsInfo: ({ commit }, info) => {
            logger.info('AfterSale => 设置商品信息', info);
            commit(SET_AFTER_SALE_GOODS_INFO, info);
        },
        // 设置整单售后信息
        setAfterSaleOrderInfo: ({ commit }, orderInfo) => {
            logger.info('AfterSale => 设置售后订单信息', orderInfo);
            commit(SET_AFTER_SALE_ORDER_INFO, orderInfo);
        },
        // 设置售后类型
        setAfterSaleType: ({ commit }, type) => {
            commit(SET_AFTER_SALE_TYPE, type);
        },
        // 售后申请
        applyAfterSaleService: async ({ commit }, params) => {
            const res = await post(SHOP_URL.refundApply, params);
            return res ? res.apply_number : false;
        },
        // 获取售后单详情
        getAfterSaleOrderDetail: async ({ commit }, applyNumber) => {
            const res = await get(SHOP_URL.refundDetail, { apply_number: applyNumber });
            logger.success('获取的售后详情 => ', res);
            commit(GET_AFTER_SALE_ORDER_DETAIL, res);
        },
        // 提交快递信息
        addAfterSaleOrderExpressInfo: async ({ commit }, params) => {
            const res = await post(SHOP_URL.refundDeliver, params);
            if (res) {
                TipSuccess('添加物流信息成功！');
            }
        },
        // 取消退货、退款申请
        cancelAfterSaleOrder: async ({ commit }, applyNumber) => {
            const res = await post(SHOP_URL.refundCancel, {
                apply_number: applyNumber,
            });
            if (res) {
                TipSuccess('取消成功！');
            }
        },
        // 获取可用配送平台
        getAvailableExpressList: async ({ commit }) => {
            const res = await get(INVENTORY_URL.express, {
                deliver_type: 2,
            });
            if (res) {
                commit(GET_AVALIABLE_EXPRESS_LIST, res.platforms);
            }
        },
    },
};

export default orderDetail;
